'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url api/pub/create_watchword 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid, tv_id, tv_name, series, source_id } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
		
		let queryCount = 1; //设置初始值为1
		while (queryCount == 1) {
			
			//生成口令
			let watchword = vk.pubfn.random(7, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");

			//查询口令是否重复
			queryCount = await vk.baseDao.count({
				dbName: "a_watchword",
				whereJson: {
					watchword: watchword
				}
			});
			
			//当生成的口令和数据库里已有口令不重复时，写入数据库
			if (queryCount == 0) {
				res.adds = await db.collection('a_watchword').add({
					tv_id: tv_id,
					tv_name: tv_name,
					series: series,
					source_id: source_id,
					watchword: watchword
				})
				res.watchword = watchword;
			}
		}

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
